package com.example.demo.component;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

import com.example.demo.context.RouteDefinitionWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author sleepforests
 * Created on 2019-07-18
 */
public class RouteFileStore {
    private static final Logger logger = LoggerFactory.getLogger(RouteFileStore.class);

    private static ObjectMapper objectMapper = new ObjectMapper();

    // 通过下面的地址可以看到加载的routes O(∩_∩)O
    // http://localhost:8080/actuator/gateway/routes


    // todo 这里特别简化的存储到json里面  可以自己整到mysql里面
    public static RouteDefinitionWrapper get() {
        try {
//            File file = ResourceUtils.getFile("classpath:route.json");
            File file = ResourceUtils.getFile("file:/Users/wangbing05/github.com/spring-cloud-gateway-demo/example-scg/src/main/resources/route.json");


            String json = FileUtils.readFileToString(file);
            RouteDefinitionWrapper routeDefinitionWrapper = objectMapper.readValue(json,
                    RouteDefinitionWrapper.class);

            return routeDefinitionWrapper;

        } catch (Exception e) {
            logger.error("get route.json error", e);
        }


        return null;
    }


}
